﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Globalization;
using System.Net;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Runtime.Serialization;
using BaoJianSoft.Common.Properties;

namespace BaoJianSoft.Common.Web
{
    /// <summary>
    /// Restful error behavior.
    /// </summary>
    public class RestfulErrorBehavior : IErrorHandler
    {

        #region IErrorHandler Members

        /// <summary>
        /// Enables error-related processing and returns a value that indicates whether
        /// subsequent HandleError implementations are called.
        /// </summary>
        /// <param name="error">The exception thrown during processing.</param>
        /// <returns>true if subsequent System.ServiceModel.Dispatcher.IErrorHandler implementations must not be called; otherwise, false. The default is false.</returns>
        public bool HandleError(Exception error)
        {
            // Allow subsequent IErrorHanlder can be called.
            return false;
        }

        /// <summary>
        /// Enables the creation of a custom error message.
        /// </summary>
        /// <param name="error">The System.Exception object thrown in the course of the service operation.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <param name="fault">The System.ServiceModel.Channels.Message object that is returned to the client, or service, in the duplex case.</param>
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            RestfulException restfulException = error as RestfulException;
            if (restfulException == null)
            {
                if (error is SerializationException)
                {
                    restfulException = new BadRequestException(Resources.DeserializationFailMessage, error);
                }
                else
                {
                    restfulException = new InternalServerErrorException(Resources.InternalServerErrorMessage, error);
                }
            }

            fault = new RestfulErrorMessage(restfulException);
        }

        #endregion
    }
}

